package com.dormitory.Servlet;
import com.dormitory.Service.UserLoginService;
import com.dormitory.Service.UserService;
import com.dormitory.dao.UserConnectimpl;
import com.dormitory.entity.UserLogin;
import com.mysql.cj.Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class loginServlet extends HttpServlet {
    private UserService userService =new UserLoginService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password = req.getParameter("password");
        UserLogin userLogin = this.userService.login(username, password);
        switch (userLogin.getCode()){
            case -1:
                req.setAttribute("usernameError","用户不存在");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
                break;
            case -2:
                req.setAttribute("passwordError","密码错误");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
                break;
            case 0:
                System.out.println("登录成功");
                req.setAttribute("name",userLogin.getName());
                req.setAttribute("limit",userLogin.getLimit());
                req.getRequestDispatcher("index.jsp").forward(req,resp);
                break;
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

